package com.steshov.trainer.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.steshov.trainer.R;

public class App {
	public final static int colNum = 5;
	public final static int CREATE_TASK_MODE = 0;
	public final static int EDIT_TASK_MODE = 1;
	public static int taskMode = CREATE_TASK_MODE;
	public static int taskMenuMode = CREATE_TASK_MODE;
	public final static int APPLICATION_IN_BACKGROUND = 0;
	public final static int APPLICATION_TERMINATE = 1;
	public static int applicationMode = APPLICATION_TERMINATE;
	public static int selectedOptionItemId;
	public final static String Finished = "Finished!";
	public static Boolean generateButtonFlag;

	public static void msg(Context context, String msg) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setMessage(msg);
		alert.show();
	}

	public static int getRowNumberByIndex(int index) {
		int rownumber = 0;
		while (index - colNum > 0) {
			index -= colNum;
			rownumber++;
		}
		return rownumber;
	}

	public static long getMiliseconds(DatePicker datePicker,
			TimePicker timePicker) {
		int year = datePicker.getYear();
		int month = datePicker.getMonth();
		int day = datePicker.getDayOfMonth();
		int hour = timePicker.getCurrentHour();
		int minutes = timePicker.getCurrentMinute();

		final Calendar c = Calendar.getInstance();
		c.set(year, month, day, hour, minutes);
		long timestamp = c.getTimeInMillis();
		return timestamp;
	}

	public static String convertToTimeLeft(long originalFutureTime) {
		Date left = new Date(originalFutureTime - new Date().getTime());
		long milliseconds = left.getTime();
		if (milliseconds > 0) {
			int seconds = (int) TimeUnit.MILLISECONDS.toSeconds(milliseconds) % 60;
			int minutes = (int) TimeUnit.MILLISECONDS.toSeconds(milliseconds) / 60 % 60;
			int hours = (int) TimeUnit.MILLISECONDS.toSeconds(milliseconds) / 3600;
			return String.format("%d:%d:%d", hours, minutes, seconds);
		} else {
			return Finished;
		}
	}

	public static void initOptMenu(Menu menu) {
		if (selectedOptionItemId != 0) {
			((MenuItem) menu.findItem(selectedOptionItemId)).setChecked(true);
		}
	}

	public static void onOptionsItemSelectedThing(MenuItem item,
			Activity context) {
		if (item.getItemId() == R.id.exit) {
			switch (applicationMode) {
			case APPLICATION_IN_BACKGROUND:
				context.finish();
				break;
			case APPLICATION_TERMINATE:
				System.exit(0);
				break;
			}

		} else if (item.getItemId() == R.id.wbg) {
			applicationMode = APPLICATION_IN_BACKGROUND;
			item.setChecked(true);
			selectedOptionItemId = item.getItemId();
		} else if (item.getItemId() == R.id.off) {
			applicationMode = APPLICATION_TERMINATE;
			item.setChecked(true);
			selectedOptionItemId = item.getItemId();
		}
	}

	public static boolean commandContains(String element) {
		for (Command e : Command.values()) {
			if (e.toString().equals(element)) {
				return true;
			}
		}
		return false;
	}

	public static long fromTimeStringToMilis(String timeString) {
		int hours = Integer.parseInt(timeString.split(":")[0]) * 3600;
		int minutes = Integer.parseInt(timeString.split(":")[1]) * 60;
		int seconds = Integer.parseInt(timeString.split(":")[2]);
		long t = (hours + minutes + seconds) * 1000;
		return t;
	}



	

}
